package com.probiz.estoresf.product.web.action;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.common.model.customer.Consult;
import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.product.ProductAttGroup;
import com.probiz.estore.common.model.product.ProductMedia;
import com.probiz.estore.common.model.product.ProductReview;
import com.probiz.estore.common.model.product.ProductSku;
import com.probiz.estore.core.controller.GenericStoreFrontController;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.core.view.DownloadView;
import com.probiz.estore.customer.model.ConsultSearchCriteria;
import com.probiz.estore.customer.service.ConsultManager;
import com.probiz.estore.product.model.ProductReviewSearchCriteria;
import com.probiz.estore.product.service.ProdMainManager;
import com.probiz.estore.product.service.ProdMainNtManager;
import com.probiz.estore.product.service.ProductManager;
import com.probiz.estore.product.service.ProductMediaManager;
import com.probiz.estore.product.service.ProductReviewManager;
import com.probiz.estore.product.service.ProductSkuManager;
import com.probiz.estore.webapp.util.RequestUtil;

public class ProductFrontController extends		GenericStoreFrontController<Product> {
	private ProductManager	productManager;
	private ProdMainManager	prodMainManager;
	private ProdMainNtManager prodMainNtManager;
	private ProductSkuManager productSkuManager;
	private ProductReviewManager productReviewManager;
	private ConsultManager consultManager;
	private ProductMediaManager productMediaManager;
	protected void initController() throws Exception {
		mgr = productManager;
	}
	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}
	
	public void setProductSkuManager(ProductSkuManager productSkuManager) {
		this.productSkuManager = productSkuManager;
	}
	public void setProdMainManager(ProdMainManager prodMainManager) {
		this.prodMainManager = prodMainManager;
	}	
	
	public void setProdMainNtManager(ProdMainNtManager prodMainNtManager) {
		this.prodMainNtManager = prodMainNtManager;
	}
	public void setProductReviewManager(ProductReviewManager productReviewManager) {
		this.productReviewManager = productReviewManager;
	}
	public void setConsultManager(ConsultManager consultManager) {
		this.consultManager = consultManager;
	}
	public void setProductMediaManager(ProductMediaManager productMediaManager) {
		this.productMediaManager = productMediaManager;
	}
	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) {
		// -----start:获得产品------
		Integer productId = productManager.getProductIdByUri(request.getRequestURI());
		Product product = productManager.getById(productId);
		// 没有找到相应产品或草稿状态的不能访问,直接进入找不到或已删除页面
		if (product == null	|| product.getStatus().shortValue() != Product.STATUS_ACTIVE.shortValue()) {
			return new ModelAndView("product/productUnAvailable");
		}
		// -----end:获得产品------
		// -----start:获得产品属性组和属性------
		List<ProductAttGroup> productAttGroups = prodMainManager.getProductAttGroupsAndValues(product);
		// -----end:获得产品属性组和属性------

		// -----start:获得前10位的商品评论------
		SearchCriteria searchCriteria = this.createSearchCriteria(request);
		searchCriteria.changePaging(1, 10);
		ProductReviewSearchCriteria productReviewSearchCriteria = new ProductReviewSearchCriteria();
		productReviewSearchCriteria.setProductId(productId);
		productReviewSearchCriteria.setOrderby((short)0);
		productReviewSearchCriteria.setStatus(ProductReview.STATUS_SHOW);
		List<ProductReview>  productReviewList = productReviewManager.searchByCriteria(productReviewSearchCriteria, searchCriteria);
		// -----end:  获得前10位的商品评论------
		
		
		// -----start:获得前10条的商品咨询------
		SearchCriteria searchCriteria2 = this.createSearchCriteria(request);
		searchCriteria2.changePaging(1, 10);
		ConsultSearchCriteria consultSearchCriteria = new ConsultSearchCriteria();
		consultSearchCriteria.setProductId(productId);
		consultSearchCriteria.setOrderby((short)0);
		consultSearchCriteria.setStatus(Consult.STATUS_SHOW);
		List<Consult>  consultList = consultManager.searchByCriteria(consultSearchCriteria, searchCriteria2);
		// -----end:  获得前10位的商品咨询------
		
		
	
		
		ModelAndView mv = new ModelAndView("product/defaultProductDetail");
		mv.addObject("product",product);
		mv.addObject("category",product.getCategory());
		mv.addObject("productAttGroups",productAttGroups);
		mv.addObject("productReviewList",productReviewList);
		mv.addObject("consultList",consultList);
		mv.addObject("mapOfRelatedSkuOptionAndValues", prodMainManager.findMapOfRelatedSkuOptionAndValues(product));
		mv.addObject("jsonProduct",prodMainManager.getProductJSONDataForFront(product));
		// -----start:获得好中差评及五星次数------
		mv.addObject("scoreSpanCounts",productReviewManager.getCountsByScoreSpanAndProductId(product.getProductId()));
		// -----end:  获得好中差评及五星次数------
		
		//start:记录点击率
		prodMainNtManager.doRecordViewCount(productId, request.getSession());
		//end:  记录点击率
		return mv;
	}
	/**
	 * 渲染价格区块
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView renderProductSkuPrice(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv = new ModelAndView("product/include/productDetailPrice");
		ProductSku productSku = productSkuManager.getById(RequestUtil.getInteger(request, "productSkuId"));
		mv.addObject("productSku",productSku);
		return mv;
	}
	
	/**
	 * 下载附件
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView downloadFile(HttpServletRequest request,HttpServletResponse response) throws ServletException {
        Integer mediaId = RequestUtil.getInteger(request, "mediaId");
        ProductMedia media = productMediaManager.getById(mediaId);
        String path = ConfigUtil.getInstance().getMediaStorePath(media.getMediaUrl());
		ModelAndView mv = new ModelAndView(new DownloadView(path,media.getMediaTitleWithMimeType()));
        return mv;
               
	}
	
	
}
